/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rev1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 *
 * @author Administrator
 */
public class CobaBaca {

    File name;
    String directory[];
    private Scanner input;

    public CobaBaca() {
    }

    public boolean openDirectory(String path) {
        name = new File(path);
        if (name.isDirectory()) // apa
        {
            directory = name.list();
            return true;
        } // end else
        else {
            return false;
        }

    }

    // enable user to open file
    public void openFile(String namaPathFile) {
        try {
            input = new Scanner(new File(namaPathFile));
        } // end try
        catch (FileNotFoundException fileNotFoundException) {
            System.err.println("Error opening file.");
            System.exit(1);
        } // end catch

    } // end method openFile

    // read record from file
    public void readRecords(String inFileName) {
        try // read records from file using Scanner object
        {
            while (input.hasNext()) {
                StringTokenizer tokens = new StringTokenizer(input.nextLine());
                while (tokens.hasMoreTokens()) {
                    String temp = tokens.nextToken().replaceAll(",", "");
                    String temp2 = temp.replaceAll("'", "");
                    temp = temp2.replaceAll("\"", "");
                    if (temp.length() != 0) {

//                        System.out.println(temp2);
                        Stemmer st = new Stemmer();
                        st.setKata(temp);
                        System.out.println(st.getKata());
                    }
                }
            } // end while

        } // end try
        catch (NoSuchElementException elementException) {
            System.err.println("File improperly formed.");
            input.close();
            System.exit(1);
        } // end catch
        catch (IllegalStateException stateException) {
            System.err.println("Error reading from file.");
            System.exit(1);
        } // end catch

    } // end method readRecords

    public void closeFile() {
        if (input != null) {
            input.close(); // close file

        }
    } // end method closeFile

    public void createData(String path) {


        if (openDirectory(path)) {
            for (String fileName : directory) {
                openFile(path + "\\" + fileName);
                readRecords(fileName);
                closeFile();
            }


        } else {
            System.out.println("Direktori tidak ditemukan");
        }
    }

    public static void main(String[] args) throws IOException {
        CobaBaca cb = new CobaBaca();
        cb.createData("sample");
    }
}
